package platform.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.spring.JAXRSServerFactoryBeanDefinitionParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Restful配置
 *
 * @author roamer
 * @version V1.0
 * @date 2019-02-24 20:11
 */
@Configuration
@Import({ObjectMapperConfigContext.class})
public class RestConfigContext {

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 将JAXRS交给Spring管理生命周期
     *
     * @return {@code SpringBus}
     */
    @Bean(destroyMethod = "shutdown")
    public SpringBus cxf() {
        return new SpringBus();
    }

    /**
     * 用于创建JAXRSServer提供Restful服务
     *
     * @return {@code JAXRSServerFactoryBean}
     */
    @Bean
    public JAXRSServerFactoryBean jaxrsServerFactoryBean() {
        JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBeanDefinitionParser.SpringJAXRSServerFactoryBean();
        // 设置访问路径
        factoryBean.setAddress("/");
        factoryBean.setProviders(providerConfigurer().getProviders());
        return factoryBean;
    }

    @Bean
    public ServiceProviderConfigurer providerConfigurer() {
        return new ServiceProviderConfigurer(objectMapper);
    }

}
